/**
 * Opens a topmost browser window
 * @param options Object
 * @returns jQuery object
 */
(function($) {
	$.fn.popup = function(options) {
		var popup_window;
		if(!options) {
			return $();
		}
		if(!options.url || options.url == "") {
			return $();
		}
		if(!options.default_page) {
			options.default_page = 'loading.php';
		}
		if(options.data != '') {
			var options_string = '';
			if(options.height) {
				options_string+= "height=" + options.height + ",";
				options.pad = "?pad="+((options.height / 2) - 20);
			} else {
				options.pad = "";
			}
			if(options.width) {
				options_string += "width=" + options.width + ",";
			}
			if(options.top) {
				options_string += "top=" + options.top + ",";
			} else {
				options_string += "top=0,";
			}
			if(options.left) {
				options_string += "left=" + options.left + ",";
			} else {
				options_string += "left=0,";
			}
			options_string += "location=no, menubar=no, scrollbars=yes, status=no";
			popup_window = window.open(options.default_page+options.pad, '_blank', options_string);
		}
		
		$.post(
			options.url,
			options.data,
			function (data, textStatus) {
				if(popup_window.self){
					with (popup_window.document) {
						open("text/html", "replace");
						write(data);
						close();
					}
					return $(popup_window);
				} else {
					return $();
				}
			}
		);
		return $();
	};
})(jQuery);